<?php
/*********************************************************
author			Henson
date			2011-3-30
function		生成首页
*********************************************************/

include_once("../management/admin_common.inc.php");
include_once( _INC_PATH."database.php" );

include_once( _INC_PATH."catecae.php" );
include_once( _INC_PATH."global.php" );
$smarty->assign("cateSubCae",$cateSubCae);
$smarty->assign("cor_scale_arr",$cor_scale_arr);
$smarty->assign("cor_type_arr",$cor_type_arr);
$db = new database();
$result			= getEstimateCorporation();
$result_hot		= getEstimateByHotCorporation();	
$result_good	= getEstimateByGoodCorporation();
$result_links	= getLinks();
$smarty->assign("result",$result);
$smarty->assign("result_hot",$result_hot);
$smarty->assign("result_good",$result_good);
$smarty->assign("linkRows",$result_links);
$smarty->assign("array_number",$array_number);
$smarty->assign("techCate",getTech());
$smarty->assign("template_name",'index.tpl');
$pageMain = $smarty->fetch("layout.tpl");
$url=_ROOT."/index.html";
writeFile($pageMain,$url);
echo $pageMain;
//header("Content-type: text/html; charset=utf-8"); 
//echo "created OK ! &nbsp;&nbsp; <a href='/index.html'>进入首页</a>";
die;


function getEstimateCorporation()
{
	global $db;
	$sql = "select recom_id from tbl_recom where type='sypl' and del_flg='0'";
	$db->setQuery($sql);
	$recom_ids = $db->loadResultList();
	$sql="SELECT c.id,e.content,e.salary,e.environment,e.opportunity,e.pressure,e.user_name,e.user_id,c.name,c.address
		  FROM tbl_estimate e 
		  LEFT JOIN tbl_corporation c ON e.cor_id=c.id
		  WHERE e.del_flg='0' and e.id in ('".implode("','", $recom_ids)."') limit 5";
	$db->setQuery($sql);
	$data = $db->loadList();
	if(!empty($data) && is_array($data))
	{
		foreach($data as $key=>$value)
		{
			$data[$key]['content'] = str_replace("&lt;br /&gt;","<br/>",$data[$key]['content']);
			$data[$key]['content'] = str_replace("&amp;nbsp;"," ",$data[$key]['content']);
			$data[$key]['content'] = preg_replace('#&lt;img src=&quot;'._HOST_ROOT.'/images/face/(\d{1,2}).gif&quot; /&gt;#','<img src="'._HOST_ROOT.'/images/face/$1.gif"/>',$data[$key]['content']);
			if($value['user_name'] == _VISITOR){
				$data[$key]['faceImg'] = '/images/87face.gif';
			}elseif(findImg($value['user_id'],"face")){
				$data[$key]['faceImg'] = findImg($value['user_id'],"face");
			}else{
				$data[$key]['faceImg'] = '/images/noface.gif';
			}
		}
	}
	return $data;
}

//好评企业
function getEstimateByGoodCorporation()
{
	global $db;
	$sql="SELECT SUM(salary + environment + opportunity - pressure +0) AS sum_auto,c.id,c.name
		  FROM tbl_estimate e 
		  LEFT JOIN tbl_corporation c ON e.cor_id=c.id
		  WHERE e.del_flg='0' GROUP BY id,name ORDER BY sum_auto desc limit 10";
	$db->setQuery($sql);
	$data = $db->loadList();
	return $data;
}
//热门企业
function getEstimateByHotCorporation()
{
	global $db;
	$sql="SELECT count(c.id) as count_auto,c.id,c.name
		  FROM tbl_estimate e 
		  LEFT JOIN tbl_corporation c ON e.cor_id=c.id
		  WHERE e.del_flg='0' GROUP BY id ORDER BY count_auto desc limit 10";
	$db->setQuery($sql);
	$data = $db->loadList();
	return $data;
}

function getTech()
{
	global $db;
	$sql = "select * from tbl_tech_cate where del_flg='0'";
	$db->setQuery($sql);
	$cateRows = $db->loadList();
	if(is_array($cateRows) && count($cateRows)>0)
	{
		foreach($cateRows as $key=>$value)
		{
			$sql = "select * from tbl_tech where cid='".$value['id']."' order by mTime desc limit 10";
			$db->setQuery($sql);
			$techRows = $db->loadList();
			if(is_array($techRows) && count($techRows)>0){
				foreach($techRows as $techK=>$techV)
				{
					$techRows[$techK]['link'] = 'http://www.87dianping.com/tech/'.$techV['id'].".html";
					$techRows[$techK]['date'] = date("Y-m",$techV['mTime']);
				}
				$cateRows[$key]['techRows'] = $techRows;
			}else{
				unset($cateRows[$key]);
			}
		}
	}
	return $cateRows;
}
function getLinks()
{
	global $db;
	$sql = "select title,link from tbl_recom where del_flg='0' and type='links'";
	$db->setQuery($sql);
	return $db->loadList();
}
?>